package com.wq.gof.designer.command;

/**
 * @Author: wangqiang20995
 * @Date:2019/1/25
 * @Description:
 * @Resource:
 */
public class Receiver {

    private Wheel wheel;

    private Signal lastAction;

    public Receiver(Wheel wheel){
        this.wheel = wheel;
    }

    public void execute(Signal signal){

        synchronized (this){
            this.lastAction = signal;
        }

        switch (signal){
            case LEFT: wheel.left();break;
            case AHEAD: wheel.goAhead();break;
            case RIGHT: wheel.right();break;
            case BACKOF: wheel.backOf();break;
        }
    }

    public void undo(){
        if(lastAction == null){
            System.out.println("这是第一个动作，不能为撤销，故上一个动作为空");
        }else {
            System.out.println("撤销上一个动作：" + lastAction.name());
        }
    }
}
